//
//  PTLocation.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/26.
//

/**
 系统会自动分配的 而且还会有缓存

 iOS设备能提供3种不同途径进行定位：Wifi, 蜂窝式移动电话基站, GPS卫星
 iOS 不像Android系统在定位服务编程时，可以指定采用哪种途径进行定位。iOS的API把底层这些细节屏蔽掉了，开发人员和用户并不知道现在设备是采用 哪种方式进行定位的，iOS系统会根据设备的情况和周围的环境，采用一套最佳的解决方案。这个方案是这样的，如果能够接收GPS信息，那么设备优先采用 GPS定位，否则采用Wifi或蜂窝基站定位，在Wifi和蜂窝基站之间优先使用Wifi，如果无法连接Wifi才使用蜂窝基站定位。
 原文链接：https://blog.csdn.net/ochenmengo/article/details/82880872
 */

import SwiftUI
import CoreLocation

struct HorizontalLine: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: rect.minX, y: rect.midY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
        return path
    }
}

struct PTLocation: View {
    
    @State private var locationManager = CLLocationManager()
    @State private var lat = ""
    @State private var lng = ""
    @State private var err = "暂无"
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "不支持开发功能"
    
    var body: some View {
        ZStack {
            ScrollView {
                VStack {
                    HStack {
                        VStack(alignment: .leading) {
                            Text("开启位置服务")
                                .font(.system(size: 16))
                                .padding(.leading,10)
                            Text("经度：\(lng)")
                                .font(.system(size: 16))
                                .padding(.top,10)
                                .padding(.leading,10)
                            Text("纬度：\(lat)")
                                .font(.system(size: 16))
                                .padding(.top,20)
                                .padding(.leading,10)
                            Text("错误：\(err)")
                                .font(.system(size: 16))
                                .padding(.top,20)
                                .padding(.leading,10)
                        }
                        Spacer()
                    }
                    .padding(.bottom,10)
                    
                    HorizontalLine().stroke(Color.gray, lineWidth: 1)
                        .frame(height: 1)
                        .padding(EdgeInsets(top: 10, leading: 10, bottom: 20, trailing: 10))
                    HStack {
                        Text("百度")
                        Spacer()
                        Button {
                            getLocationFromNetwork()
                        } label: {
                            Text("网络定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        
                        
                        Button {
                            getLocationFromGPS()
                        } label: {
                            Text("卫星定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding(.leading,10)
                        
                    }.padding(EdgeInsets(top: 0, leading: 10, bottom: 10, trailing: 10))
                    
                    
                    HStack {
                        Text("高德")
                        Spacer()
                        Button {
                            getLocationFromNetwork()
                        } label: {
                            Text("网络定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        
                        Button {
                            getLocationFromGPS()
                        } label: {
                            Text("卫星定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding(.leading,10)
                        
                    }.padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                    
                    HStack {
                        Text("腾讯")
                        Spacer()
                        Button {
                            getLocationFromNetwork()
                        } label: {
                            Text("网络定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        
                        Button {
                            getLocationFromGPS()
                        } label: {
                            Text("卫星定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding(.leading,10)
                        
                    }.padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                    
                    HStack {
                        Text("原生api")
                        Spacer()
                        Button {
                            getLocationFromNetwork()
                        } label: {
                            Text("网络定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        
                        Button {
                            getLocationFromGPS()
                        } label: {
                            Text("卫星定位")
                                .frame(width: 40)
                                .foregroundColor(.blue)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color.blue, lineWidth: 1)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding(.leading,10)
                        
                    }.padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                    Spacer()
                }
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }
    }
    
    func getLocationFromNetwork() {
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
            if let location = locationManager.location {
                print("网络-经度: \(location.coordinate.longitude), 纬度: \(location.coordinate.latitude)")
                lat = "\(location.coordinate.latitude)"
                lng = "\(location.coordinate.longitude)"
            }
        }
    }
    
    func getLocationFromGPS() {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
            if let location = locationManager.location {
                print("gps-经度: \(location.coordinate.longitude), 纬度: \(location.coordinate.latitude)")
                lat = "\(location.coordinate.latitude)"
                lng = "\(location.coordinate.longitude)"
            }
        }
    }
}

struct PTLocation_Previews: PreviewProvider {
    static var previews: some View {
        PTLocation()
    }
}
